/*
 * 描述
 * Created by: litangyu
 * 包含类名列表
 * 版本信息，版本号 v1.0
 * 创建日期 16/8/29
 * 版权声明
*/
package com.foton.logistic.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.foton.logistic.R;
import com.foton.logistic.base.BaseActivity;
import com.foton.logistic.listener.IOnResultListener;
import com.foton.logistic.model.login.LoginResultEntity;
import com.foton.logistic.task.DispatchTask;
import com.foton.logistic.task.ShowDialogTask;
import com.foton.logistic.task.TaskConstant;
import com.foton.logistic.util.BaseConstant;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.SharedUtil;
import com.foton.logistic.util.net.HttpUtil;
import com.foton.logistic.util.tool.RegexUtil;
import com.foton.logistic.util.tool.StringUtil;

import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.Optional;


/**
 * 描述
 * 登录界面
 * Created by: litangyu<br/>
 * Created on: 16/8/29 下午2:32
 */
public class LoginActivity extends BaseActivity {
    @InjectView(R.id.ft_ui_et_username)
    public EditText mUserName;
    @InjectView(R.id.ft_ui_et_password)
    public EditText mPassWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setTaskTag(getClass().getSimpleName());
        setContentView(R.layout.ft_ui_login);
        ButterKnife.inject(this);
    }

    @Override
    public void init() {
        setTitleText(getString(R.string.login));
        setTitleTextVisibility(View.VISIBLE);

        String username = SharedUtil.getTel(this);
        if (!StringUtil.isEmpty(username)) {
            mUserName.setText(username);
        }
    }

    @Optional
    @OnClick({R.id.ft_ui_btn_login})
    public void onClick() {
        String userName = mUserName.getText().toString().trim();
        String passWord = mPassWord.getText().toString().trim();

        if (StringUtil.isEmpty(userName)) {
            OptionUtil.addToast(this, getString(R.string.username_is_null));
            return;
        }

        if (StringUtil.isEmpty(passWord)) {
            OptionUtil.addToast(this, getString(R.string.password_is_null));
            return;
        }

        if (RegexUtil.checkElevenMobile(userName)) {
            if (!StringUtil.isEmpty(passWord)) {
                //发起网络请求
                login(true, userName, passWord);
            } else {
                OptionUtil.addToast(this, getString(R.string.password_error));
            }
        } else {
            OptionUtil.addToast(this, getString(R.string.username_error));
        }
    }

    private void login(boolean showDialog, final String userName, String password) {
        OptionUtil.closeKeyBoard(this);

        Map<String, Object> argMap = HttpUtil.getEncryMap();
        argMap.put("tel", userName);
        argMap.put("password", password);
        argMap.put("userType", "0");
        ShowDialogTask task = new ShowDialogTask(this, taskTag, mPassWord, getString(R.string
                .logging), showDialog,
                BaseConstant.LOGIN, argMap, TaskConstant.POST);
        task.setParseClass(LoginResultEntity.class);
        task.setiOnResultListener(new IOnResultListener() {
            @Override
            public void onOK(DispatchTask task) {
                OptionUtil.addToast(LoginActivity.this, getString(R.string.login_success));
                LoginResultEntity loginResult = (LoginResultEntity) task.resultEntity;
                String token = loginResult.getData();
                String[] split = token.split("-");
                String userId = split[0];
                SharedUtil.saveLogin(LoginActivity.this, true);
                SharedUtil.saveTel(LoginActivity.this, userName);
                SharedUtil.saveUserId(LoginActivity.this, userId);
                SharedUtil.saveToken(LoginActivity.this, token);
                SharedUtil.savePushSet(LoginActivity.this, false);
                //BaseConstant.COMPUL=false;
                startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                finishSelf();
            }

            @Override
            public void onError(DispatchTask task) {

            }

            @Override
            public void onDone(DispatchTask task) {

            }
        });
        task.execute();
    }

    @OnClick({R.id.ft_ui_btn_forget, R.id.ft_ui_btn_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ft_ui_btn_forget:
                Intent intent1 = new Intent(this, SmsConfirmActivity.class);
                startActivity(intent1);
                break;
            case R.id.ft_ui_btn_register:
                Intent intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }


}
